package com.example.android.utils.models

import java.lang.Exception

/**
 * @ClassName Form
 * @Author Ryan Kamanri
 * @Date 2022/6/1
 * @Description 作为发送和接收数据时的动态对象
 **/
class Form(vararg properties: Pair<String, Any>) : HashMap<String, Any>() {
    init {
        properties.forEach {
            this[it.first] = it.second
        }
    }

    fun Get(key: String): Form {
        return this[key] as Form
    }

    fun <T> Get(key: String): T {
        return this[key] as T
    }

    fun <T> AsMyResult(): MyResult<T> {
        var myResult: MyResult<T> = MyResult()
        kotlin.runCatching {
            myResult = MyResult(this.Get<Int>("code"), this.Get<String>("message"), this.Get<T>("data"))
        }.fold({
            return myResult
        }, {
            it.printStackTrace()
            throw ClassCastException("Failed To Cast Form To MyResult Caused By: \n ${it.message}")
        })
    }

}